﻿using CPPEI.Coolzon.UI.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI
{
    /// <summary>
    /// 组件工作的上下文,每一个PartFrame具有一个PartContext
    /// 组件的属性注入，初始化，激活，激活到非激活都需要用到此对象里的信息
    /// </summary>
    public class PartContext
    {
        private static string s_Identity = Guid.NewGuid().ToString("N");

        public string Identity
        {
            get
            {
                return s_Identity;
            }
        }

        /// <summary>
        /// 脚本引擎
        /// </summary>
        public ScriptEngine ScriptEngine { get; private set; }

        /// <summary>
        /// 当前运行的解决方案
        /// </summary>
        public Metadata.Solution SolutionMetadata { get; private set; }

        /// <summary>
        /// 当前显示的场景
        /// </summary>
        public Metadata.Scene SceneMetadata { get; set; }

        /// <summary>
        /// 是否显示activeX组件,默认值为true
        /// </summary>
        public bool IsShowActiveXPart { get; set; }

        /// <summary>
        /// 场景打开操作的参数
        /// </summary>
        public OpenSceneArgs OpenSceneArgs { get; set; }

        /// <summary>
        /// 是否为预加载数据
        /// true:在控件初始化时同时加载数据,不往显示界面上更新,
        /// false:控件显示时加载数据，需要往界面上更新
        /// </summary>
        public bool IsPreviewLoadData { get; set; }

        /// <summary>
        /// 是否是根级的PartContext，
        /// 如果是弹出的场景中的PartContext此属性应该为false;
        /// </summary>
        public bool IsRoot { get; set; }

        /// <summary>
        /// 缓存模式
        /// ，默认是NoCache
        /// </summary>
        public ResourceCacheMode ResourceCacheMode { get; set; }

        public Metadata.DataCacheMode DataCacheMode { get; private set; }

        public static string AppType { get; set; }

        /// <summary>
        /// 构造组件运行的上下文信息
        /// </summary>
        /// <param name="engine">引擎实例</param>
        /// <param name="solutionMetadata">方案元数据</param>
        /// <param name="sceneMetadata">场景元数据</param>
        public PartContext(ScriptEngine engine, Metadata.Solution solutionMetadata, Metadata.Scene sceneMetadata)
        {
            ScriptEngine = engine;
            SolutionMetadata = solutionMetadata;
            SceneMetadata = sceneMetadata;

            IsShowActiveXPart = true;
            IsRoot = true;

            #region 设置缓存模式
            if (solutionMetadata != null)
            {
                if (solutionMetadata.PartCacheMode == Metadata.PartCacheMode.NoCache)
                {
                    ResourceCacheMode = UI.ResourceCacheMode.NoCache;
                }
            }
            ResourceCacheMode = UI.ResourceCacheMode.NoCache;
            #endregion

            DataCacheMode = solutionMetadata.DataCacheMode;
        }

        /// <summary>
        /// 清除上下文中关联的信息
        /// </summary>
        public void Clear()
        {
            OpenSceneArgs = null;
            if (ScriptEngine != null)
            {
                ScriptEngine.Close();
                ScriptEngine = null;
            }
            SolutionMetadata = null;
            SceneMetadata = null;
        }
    }
}
